package com.smarttourism.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smarttourism.common.core.domain.R;
import com.smarttourism.domain.Room;
import com.smarttourism.service.RoomService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;


    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    public R findbyid(@PathVariable("id") Integer id){
        Room room = roomService.findById(id);
        return room != null ?R.ok(room):R.fail("新增失败");

    }
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    public R findAll(Integer rNum,String typeName,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);

        List<Room> list = roomService.findAll(rNum,typeName);
        PageInfo<Room> roomPageInfo = new PageInfo<>(list);
        return roomPageInfo != null?R.ok(roomPageInfo):R.fail("查询失败");
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    public R add(@RequestBody Room room){
        int i = roomService.add(room);
        return i>0?R.ok("新增成功"):R.fail("新增失败");

    }
    @DeleteMapping
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    public R deleteBatch(Integer[] ids){
        int i = roomService.removeBatch(ids);
        return i >0 ? R.ok("批量删除成功") : R.fail("批量删除失败");
    }
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('room:room:list')")
    public R update(@RequestBody Room room){
        int i = roomService.updateById(room);
        return i>0?R.ok("修改成功"):R.fail("修改失败");

    }

}
